package com.gzz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzz.common.R;
import com.gzz.pojo.GoodsInfo;
import com.gzz.pojo.ImgsResp;
import com.gzz.pojo.Scenery;
import com.gzz.mapper.SceneryMapper;
import com.gzz.pojo.Tickettype;
import com.gzz.service.ImgsRespService;
import com.gzz.service.SceneryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzz.service.TickettypeService;
import com.gzz.vo.GoodsVo;
import com.gzz.vo.SceneryVo;
import com.gzz.vo.SenceryDetailVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.gzz.utils.OssUtils.GoodTypeId;
import static com.gzz.utils.OssUtils.ViewTypeId;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 周佳成
 * @since 2023-11-20
 */
@Service
public class SceneryServiceImpl extends ServiceImpl<SceneryMapper, Scenery> implements SceneryService {

	@Autowired
	TickettypeService tickettypeService;
	@Resource
	ImgsRespService imgsRespService;
	@Resource
	SceneryService sceneryService;

	@Override
	public R<SenceryDetailVo> getDetailbyId(Long scId) {
		LambdaQueryWrapper<Tickettype> tickettypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
		tickettypeLambdaQueryWrapper.eq(Tickettype::getScId,scId);
		List<Tickettype> typelist = tickettypeService.list(tickettypeLambdaQueryWrapper);

		LambdaQueryWrapper<ImgsResp> imgsRespLambdaQueryWrapper = new LambdaQueryWrapper<>();
		imgsRespLambdaQueryWrapper.eq(ImgsResp::getTypeId, ViewTypeId).eq(ImgsResp::getStId,scId);
		List<ImgsResp> imgsRespList = imgsRespService.list(imgsRespLambdaQueryWrapper);

		List<String> imgs  = new ArrayList<>();

		imgsRespList.forEach((item)->{
			imgs.add(item.getImageUrl());
		});

		Scenery scenery = getById(scId);

		SenceryDetailVo senceryDetailVo = new SenceryDetailVo();

		BeanUtils.copyProperties(scenery,senceryDetailVo);

		senceryDetailVo.setScImages(imgs);

		senceryDetailVo.setTicketTypes(typelist);

		return  R.success(senceryDetailVo);

	}

	@Override
		public List<SceneryVo> getVo(List< Scenery > sceneryList) {
			List<SceneryVo> sceneryVoList = new ArrayList<>();
			for(Scenery scenery : sceneryList)
			{
				SceneryVo sceneryVo = new SceneryVo();
				BeanUtils.copyProperties(scenery,sceneryVo);
				QueryWrapper<ImgsResp> imgsRespQueryWrapper = new QueryWrapper<>();
				imgsRespQueryWrapper.eq("st_id",scenery.getScId())
						.eq("type_id",ViewTypeId);
				List<ImgsResp> imgsRespList = imgsRespService.list(imgsRespQueryWrapper);
				List<String> stringList = new ArrayList<>();
				for(ImgsResp imgsResp : imgsRespList)
				{
					stringList.add(imgsResp.getImageUrl());
				}
				sceneryVo.setImgs(stringList);
				sceneryVoList.add(sceneryVo);
			}
			return sceneryVoList;
		}
		@Override
		public boolean removeByIdAndTypeId(Long stId, Integer goodTypeId) {
			//通过id,和typeId删除数据库的图片路径
			LambdaQueryWrapper<ImgsResp> lambdaQueryWrapper = new LambdaQueryWrapper<>();
			lambdaQueryWrapper.eq(ImgsResp::getTypeId,GoodTypeId).eq(ImgsResp::getStId,stId);

			boolean r = imgsRespService.remove(lambdaQueryWrapper);
			return  r;
		}

		@Override
	public void saveVo(SceneryVo sceneryVo) {
		Scenery scenery = new Scenery();
		BeanUtils.copyProperties(sceneryVo,scenery);
		if(sceneryVo.getImgs()!=null)
			for(String url: sceneryVo.getImgs())
			{
				ImgsResp imgsResp = new ImgsResp();
				imgsResp.setImageUrl(url);
				imgsResp.setStId(scenery.getScId());
				imgsResp.setTypeId(ViewTypeId);
				imgsRespService.save(imgsResp);
			}
			sceneryService.save(scenery);
	}
}

